package com.augurit.gzcgwsp.csdlwj.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jbpm.api.ProcessEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.csdlwj.entity.YwSrDlwjSqb;
import com.augurit.gzcgwsp.csdlwj.service.IYwSrDlwjSqbService;
import com.augurit.gzcgwsp.csdlwj.web.form.YwSrDlwjSqbForm;
import com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService;
import com.augurit.gzcgwsp.sendNotice.service.IYwPublicOpinionsService;
import com.augurit.gzcgwsp.ssgd.entity.YwRqSsgdSqb;
import com.augurit.gzcgwsp.wfex.app.doc.IOaDocCodeServiceEX;
import com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX;
import com.augurit.gzcgwsp.wfex.bi.service.IWfTemplateAttachmentRefServiceEX;
import com.augurit.gzcgwsp.wfex.common.service.IWfServiceEX;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfTemplateAttachmentRefService;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.service.IWfBusClientService;
import com.augurit.wf.common.service.IWfService;
import com.augurit.wf.common.util.WfUtils;
import com.augurit.wf.common.web.WfBusClientAction;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-sr-dlwj-sqb.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/csdlwj/YwCsdlwj/YwSrCsdlwjSqb.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/gzcgwsp/csdlwj/YwCsdlwj/YwSrCsdlwjSqb-input.jsp"),
    @Result(name = WfConstant.PRINT, location = WfConstant.PRINT_JSP),
    @Result(name = WfConstant.PRINT_DIR, location = WfConstant.PRINT_DIR_JSP)
})
public class YwSrDlwjSqbAction extends WfBusClientAction<YwSrDlwjSqbForm>{

    @Autowired
    private IWfService wfService;
	@Autowired
	private IYwComQyxxService qyxxService;
    @Autowired
    private IWfBusTemplateService wfBusTemplateService;
    @Autowired
    private IWfTemplateAttachmentRefService wfTemplateAttachmentRefService;
    @Autowired
    private IWfTemplateAttachmentRefServiceEX wfTemplateAttachmentRefServiceEX;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private ISysCodeService syscodeService;
    @Autowired
	private IYwPublicOpinionsService ywPublicOpinionsService;
    @Autowired
    private IOaDocCodeService oaDocCodeService;
    @Autowired
    private IOaDocCodeServiceEX oaDocCodeServiceEX;
    @Autowired
	private IYwPrintDocService ywPrintDocService;
    @Autowired
    private IWfServiceEX wfServiceEX;
    @Autowired
    private IWfBusTemplateServiceEX wfBusTemplateServiceEX;
	
	@Autowired
	private IYwSrDlwjSqbService ywSrDlwjSqbService;

	private YwSrDlwjSqbForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<YwSrDlwjSqbForm> page = new Page<YwSrDlwjSqbForm>(10);

	   /**
     * 保存业务并关联流程实例
     */
    public String wfBusSave(){
    	try{
       
        String isYsl = super.getParameter("isYsl");
        if (form.getQyid() == null) {
            YwComQyxxForm qyxxForm = qyxxService.getFormBySqdw(form.getSqdw(),"dlwj");
            if (qyxxForm != null) {
                WfUtils.renderWfFailtureJson("该企业名称已经存在，请点击选择企业从企业列表中选择该企业！");
                return null;
            }
        }
        WfBusInstanceTaskForm task = this.wfBusSave((IWfBusClientService)ywSrDlwjSqbService, form, YwSrDlwjSqb.class.getSimpleName());
        
        
        if (StringUtils.isNotBlank(isYsl) && "1".equals(isYsl)) {
        	String executionId =  processEngine.getHistoryService().createHistoryTaskQuery().taskId(task.getTaskInstDbid().toString()).uniqueResult().getExecutionId();
            processEngine.getExecutionService().jumpToActivity(executionId, "cksa");
            ywSrDlwjSqbService.changeYslState(form.getId(), 2l);
        }
        WfUtils.renderWfBusSaveJson(task, form);
    	}catch (Exception e) {
      		WfUtils.renderWfFailtureJson("请检查输入内容长度是否过大！");
   		}
        return null;
    }
	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		ywSrDlwjSqbService.search(page, form);
		return SUCCESS;
	}
	
	public String dataList() throws Exception {
        ExtUtils.initPageFromExtGridParam(getRequest(), page);
        ywSrDlwjSqbService.search(page, form);
        renderText(Struts2Utils.getJsonString(page, DateUtils.YEAR_MOUTH_DAY_));
        return null;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		  List<SysCodeForm> ywHbbm = syscodeService.getItems("YW_RQSSGD_QY");
			getRequest().setAttribute("ywhbbmList", ywHbbm);
		return INPUT;
	}

	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save() throws Exception {
		ywSrDlwjSqbService.save(form);
		return RELOAD;
	}

	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		ywSrDlwjSqbService.delete(id);
		return RELOAD;
	}
	
	/**
	 * 删除多条记录
	 */
	@Override
	public String deleteMore() throws Exception{
		ywSrDlwjSqbService.delete(checkedIds);
		return RELOAD;
	}
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = ywSrDlwjSqbService.get(id);
		}
		else
			form = new YwSrDlwjSqbForm();
		
		  if (form.getSarq() == null) {
	            form.setSarq(new Date());
	        }
	}
	

	public YwSrDlwjSqbForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<YwSrDlwjSqbForm> getPage() {
		return page;
	}


	public void setPage(Page<YwSrDlwjSqbForm> page) {
		this.page = page;
	}
}
